/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hermit.forum.tables;

import java.sql.Connection;
import java.sql.Types;
import java.util.LinkedHashMap;
import java.util.List;
import hermit.forum.db.Table;
import hermit.forum.db.TableObject;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

/**
 *
 * @author Redin Pavel (hermit)
 */
public class User  extends Table {

 public int id = 0;
    public String sn="";
    public String basesn="";
    public String nick="";
    public String localnick="";
    public String fname="";
    public String lname="";
    public String email="";
    public String city="";
    public int sex= 0;
    public int birthyear= 0;
    public int birthmonth= 0;
    public int birthday= 0;
    public String interests="";
    public String signature="";
    public String ignor_p_users="";
    public String ignor_all_users="";
    public int personal_mes= 0;
    public int rating= 0;
    public int reputation= 0;
    public int views= 0;
    public String friends="";
    public int state = 0;
    public int status = 0;
    public int category_id=0;
    public int forum_id=0;
    public int topic_id=0;
    public int last_topic_id=0;
    public long date_reg = System.currentTimeMillis();
    public long createtime=0;
    public long lastKick = System.currentTimeMillis();
    public String user_group = "";
    public HashMap<String,Object> other_prop = new HashMap<String,Object>();
    public ArrayList<Integer> subscription = new ArrayList<Integer>();

    public User(){
        init();
        createtime = System.currentTimeMillis();
    }


    public User (int _id,
     String _sn,
     String _basesn,
     String _nick,
     String _localnick,
     String _fname,
     String _lname,
     String _email,
     String _city,
     int _sex,
     int _birthyear,
     int _birthmonth,
     int _birthday,
     String _interests,
     String _signature,
     String _ignor_p_users,
     String _ignor_all_users,
     int _personal_mes,
     int _rating,
     int _reputation,
     int _views,
     String _friends,
     int _state,
     int _status,
     int _category_id,
     int _forum_id,
     int _topic_id,
     int _last_topic_id,
     long _date_reg,
     long _createtime,
     long _lastKick,
     String _user_group,
     HashMap<String,Object> _other_prop,
     ArrayList<Integer> _subscription) {
       id = _id;
       sn=_sn;
       basesn=_basesn;
       nick=_nick;
       localnick=_localnick;
       fname=_fname;
       lname=_lname;
       email=_email;
       city=_city;
       sex=_sex;
       birthyear=_birthyear;
       birthmonth=_birthmonth;
       birthday=_birthday;
       interests=_interests;
       signature=_signature;
       ignor_p_users=_ignor_p_users;
       ignor_all_users=_ignor_all_users;
       personal_mes=_personal_mes;
       rating=_rating;
       reputation=_reputation;
       views=_views;
       friends=_friends;
       state=_state;
       status=_status;
       category_id=_category_id;
       forum_id=_forum_id;
       topic_id=_topic_id;
       last_topic_id=_last_topic_id;
       date_reg=_date_reg;
       createtime=_createtime;
       lastKick=_lastKick;
       user_group=_user_group;
       other_prop=_other_prop;
       subscription = _subscription;
       init();
    }

    public void init(){
        name="users";
        structure = new LinkedHashMap();
        structure.put("id", new TableObject(Types.INTEGER,id));
        structure.put("sn", new TableObject(Types.VARCHAR,sn));
        structure.put("basesn", new TableObject(Types.VARCHAR,basesn));
        structure.put("nick", new TableObject(Types.VARCHAR,nick));
        structure.put("localnick", new TableObject(Types.VARCHAR,localnick));
        structure.put("fname", new TableObject(Types.VARCHAR,fname));
        structure.put("lname", new TableObject(Types.VARCHAR,lname));
        structure.put("email", new TableObject(Types.VARCHAR,email));
        structure.put("city", new TableObject(Types.VARCHAR,city));
        structure.put("sex", new TableObject(Types.INTEGER,sex));
        structure.put("birthyear", new TableObject(Types.INTEGER,birthyear));
        structure.put("birthmonth", new TableObject(Types.INTEGER,birthmonth));
        structure.put("birthday", new TableObject(Types.INTEGER,birthday));
        structure.put("interests", new TableObject(Types.VARCHAR,interests));
        structure.put("signature", new TableObject(Types.VARCHAR,signature));
        structure.put("ignor_p_users", new TableObject(Types.VARCHAR,ignor_p_users));
        structure.put("ignor_all_users", new TableObject(Types.VARCHAR,ignor_all_users));
        structure.put("personal_mes", new TableObject(Types.INTEGER,personal_mes));
        structure.put("rating", new TableObject(Types.INTEGER,rating));
        structure.put("reputation", new TableObject(Types.INTEGER,reputation));
        structure.put("views", new TableObject(Types.INTEGER,views));
        structure.put("friends", new TableObject(Types.VARCHAR,friends));
        structure.put("state", new TableObject(Types.INTEGER,state));
        structure.put("status", new TableObject(Types.INTEGER,status));
        structure.put("category_id", new TableObject(Types.INTEGER,category_id));
        structure.put("forum_id", new TableObject(Types.INTEGER,forum_id));
        structure.put("topic_id", new TableObject(Types.INTEGER,topic_id));
        structure.put("last_topic_id", new TableObject(Types.INTEGER,last_topic_id));
        structure.put("date_reg", new TableObject(Types.TIMESTAMP,date_reg));
        structure.put("createtime", new TableObject(Types.TIMESTAMP,createtime));
        structure.put("lastKick", new TableObject(Types.TIMESTAMP,lastKick));
        structure.put("user_group", new TableObject(Types.VARCHAR,user_group));
        structure.put("other_prop", new TableObject(Types.JAVA_OBJECT,other_prop));
        structure.put("subscription", new TableObject(Types.JAVA_OBJECT,subscription));

    }

    @Override
    public String getTableName(){
       return this.name;
    }

    @Override
    public LinkedHashMap<String, TableObject> getStructure() {
        return structure;
    }

    @Override
    public Table getTableObject(Connection db, String q){
        User u = new User();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
            stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
            rSet = stmt.executeQuery(q);
            rSet.next();
              u.id = rSet.getInt(1);
              u.sn = rSet.getString(2);
              u.basesn = rSet.getString(3);
              u.nick = rSet.getString(4);
              u.localnick = rSet.getString(5);
              u.fname = rSet.getString(6);
              u.lname = rSet.getString(7);
              u.email = rSet.getString(8);
              u.city = rSet.getString(9);
              u.sex = rSet.getInt(10);
              u.birthyear = rSet.getInt(11);
              u.birthmonth = rSet.getInt(12);
              u.birthday = rSet.getInt(13);
              u.interests = rSet.getString(14);
              u.signature = rSet.getString(15);
              u.ignor_p_users = rSet.getString(16);
              u.ignor_all_users = rSet.getString(17);
              u.personal_mes = rSet.getInt(18);
              u.rating = rSet.getInt(19);
              u.reputation = rSet.getInt(20);
              u.views = rSet.getInt(21);
              u.friends = rSet.getString(22);
              u.state = rSet.getInt(23);
              u.status = rSet.getInt(24);
              u.category_id = rSet.getInt(25);
              u.forum_id = rSet.getInt(26);
              u.topic_id = rSet.getInt(27);
              u.last_topic_id = rSet.getInt(28);
              u.date_reg = rSet.getTimestamp(29).getTime();
              u.createtime = rSet.getTimestamp(30).getTime();
              u.lastKick = rSet.getInt(31);
              u.user_group = rSet.getString(32);
              byte[] b = rSet.getBytes(33);
              u.other_prop = (HashMap<String, Object>) TableObject.toObject(b);
              b = rSet.getBytes(34);
              u.subscription = (ArrayList<Integer>) TableObject.toObject(b);


        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        return u;
    }


    @Override
    public ArrayList<Table> getTableList(Connection db, String q){
        ArrayList<Table> list = new ArrayList<Table>();
        ResultSet rSet=null;
        Statement stmt=null;
        try{
        	stmt = db.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
        	rSet = stmt.executeQuery(q);
            while(rSet.next()) {
              User u = new User();
              u.id = rSet.getInt(1);
              u.sn = rSet.getString(2);
              u.basesn = rSet.getString(3);
              u.nick = rSet.getString(4);
              u.localnick = rSet.getString(5);
              u.fname = rSet.getString(6);
              u.lname = rSet.getString(7);
              u.email = rSet.getString(8);
              u.city = rSet.getString(9);
              u.sex = rSet.getInt(10);
              u.birthyear = rSet.getInt(11);
              u.birthmonth = rSet.getInt(12);
              u.birthday = rSet.getInt(13);
              u.interests = rSet.getString(14);
              u.signature = rSet.getString(15);
              u.ignor_p_users = rSet.getString(16);
              u.ignor_all_users = rSet.getString(17);
              u.personal_mes = rSet.getInt(18);
              u.rating = rSet.getInt(19);
              u.reputation = rSet.getInt(20);
              u.views = rSet.getInt(21);
              u.friends = rSet.getString(22);
              u.state = rSet.getInt(23);
              u.status = rSet.getInt(24);
              u.category_id = rSet.getInt(25);
              u.forum_id = rSet.getInt(26);
              u.topic_id = rSet.getInt(27);
              u.last_topic_id = rSet.getInt(28);
              u.date_reg = rSet.getTimestamp(29).getTime();
              u.createtime = rSet.getTimestamp(30).getTime();
              u.lastKick = rSet.getInt(31);
              u.user_group = rSet.getString(32);
              byte[] b = rSet.getBytes(33);
              u.other_prop = (HashMap<String, Object>) TableObject.toObject(b);
              b = rSet.getBytes(34);
              u.subscription = (ArrayList<Integer>) TableObject.toObject(b);
                list.add(u);
            }
        } catch (Exception ex){
            ex.printStackTrace();
        } finally {
        	if(rSet!=null) try{rSet.close();} catch(Exception e) {};
        	if(stmt!=null) try{stmt.close();} catch(Exception e) {};
        }
        list.trimToSize();
        return list;
    }

   

}
